package com.space.school.controller;

import com.space.school.domain.User;
import com.space.school.services.RegisterService;
import com.space.school.services.UserService;
import com.space.school.services.impl.RegisterServiceImpl;
import com.space.school.services.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "userOperateServlet", value = "/userOperateServlet")
public class UserOperateServlet extends HttpServlet {
    RegisterService registerService = new RegisterServiceImpl();
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String userId = req.getParameter("userId");
        String operate = req.getParameter("operate");
        req.getSession().setAttribute("updateUser_msg", "");
//        id的初始值  因为如果是新增条件的情况
//        userId没有赋值,为null 所以 我初始化id为0
        int id = 0;
        if (userId != null) {
            id = Integer.parseInt(userId);
        }
//       删除操作
        if (operate.equals("del")) {
            int num = userService.delete(id);
            if (num > 0) {
//                跳转页面  userServlet doget方法 Redirect 重定向
                resp.sendRedirect("/userServlet");
            }

        } else if (operate.equals("update")) {
//            先把userid 对应的值取出来
            User user = userService.findById(id);
            if (user != null) {
//                往里面防值
                req.setAttribute("user", user);
                req.getRequestDispatcher("updateUserList.jsp").forward(req, resp);
            }

        } else if (operate.equals("status")) {
            String status = req.getParameter("status");
            userService.updateStatus(id, status);
            resp.sendRedirect("/userServlet");
        } else if (operate.equals("add")) {

            req.getRequestDispatcher("insertUserList.jsp").forward(req, resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        页面提取用户id 和密码
        String userId = req.getParameter("userId");
        String userName = req.getParameter("userName");
//        修改页面的修改的新密码
        String newPassWord = req.getParameter("userNewPassword");
        String passWord = req.getParameter("passWord");
        if (userId == null) {
//            为新增操作
            boolean registerFlag = registerService.Isregister(userName);
            if (registerFlag) {
                req.getSession().setAttribute("register_msg", "你的账户已存在,请重新输入");
                //            跳转到登录页
                req.getRequestDispatcher("insertUserList.jsp").forward(req, resp);
            } else {
//            密码必须是 6位大写,小写,数字都必须有 如果不满足,你的密码设置太简单了,请重新注册
                boolean difficultyFlag = registerService.difficultyPassword(passWord);
                if (difficultyFlag) {
                    User newUser = new User();
                    newUser.setUserName(userName);
                    newUser.setPassWord(passWord);
                    userService.save(newUser);
                    resp.sendRedirect("/userServlet");
                } else {
                    req.getSession().setAttribute("register_msg", "你的密码设置太简单了,请重新注册");

                    req.getRequestDispatcher("insertUserList.jsp").forward(req, resp);
                }
            }
        } else {
//            修改操作

            User user = new User();
            user.setUserId(Integer.parseInt(userId));
            user.setPassWord(newPassWord);
            boolean difficultyFlag = registerService.difficultyPassword(newPassWord);
            if (difficultyFlag) {
                //        修改方法
                userService.update(user);
                resp.sendRedirect("/userServlet");
            } else {
//                重置 input框里的信息
//                req.setAttribute("userName",req.getParameter("userName"));
//                req.setAttribute("userOldPassword",req.getParameter("userOldPassword"));
//                旧密码
                user.setPassWord(passWord);
                user.setUserName(userName);
                req.setAttribute("user",user);
                req.getSession().setAttribute("updateUser_msg", "你的密码设置太简单了,请重新修改");
                req.getRequestDispatcher("updateUserList.jsp").forward(req, resp);
            }
        }
    }
}

